//
//  WXBundleJsPath.swift
//  mlz
//
//  Created by 吕凌 on 2018/4/28.
//  Copyright © 2018年 吕凌. All rights reserved.
//

import Foundation

public extension WXBundleJsServiceModule {
    
    @objc public func downloadToLocal(_ params: Dictionary<String, AnyObject>, callback: @escaping WXModuleKeepAliveCallback) {
        let patchFileUrl:String = params["fileUrl"]! as! String
        NSLog("更新bundljs包文件" + patchFileUrl)
        let fileManager = FileManager.default
        let bundleJsDir = BundleJsService.instance.dir()
        if(fileManager.fileExists(atPath: bundleJsDir)) {
            try! fileManager.removeItem(atPath: bundleJsDir)
        }
        try! fileManager.createDirectory(atPath: bundleJsDir, withIntermediateDirectories: true, attributes: nil)
        let filePath = bundleJsDir + "bundlejs.zip"
        let request = URLRequest(url: URL.init(string: patchFileUrl)!)
        let session = URLSession.shared
        let dataTask = session.dataTask(with: request,
                                        completionHandler: {(data, response, error) -> Void in
                                            if error != nil{
                                                callback(false, false)
                                            }else{
                                                fileManager.createFile(atPath: filePath, contents: data, attributes: nil)
                                                SSZipArchive.unzipFile(atPath: filePath, toDestination: bundleJsDir)
                                                try! fileManager.removeItem(atPath: filePath)
                                                callback(true, false)
                                            }
        }) as URLSessionTask
        //使用resume方法启动任务
        dataTask.resume()
    }
    
    @objc public func clean(_ callback: @escaping WXModuleKeepAliveCallback) {
//        print("清理本地缓存")
        let bundleJsDir = BundleJsService.instance.dir()
//        print("本地缓存目录：" + bundleJsDir)
        let fileManager = FileManager.default
        if(fileManager.fileExists(atPath: bundleJsDir)) {
            try! fileManager.removeItem(atPath: bundleJsDir)
        }
//        print("缓存目录清理成功：" + bundleJsDir)
        callback(true, false)
    }
    
    @objc public func local(_ params: Dictionary<String, AnyObject>, callback: @escaping WXModuleKeepAliveCallback) {
        let remoteJs:String = params["remote"]! as! String
        var localJs:String = params["local"]! as! String
//        print("开始加载bundleJs：" + remoteJs + " - " + localJs)
        let bundleJsDir:String = BundleJsService.instance.dir()
//        print("bundleJs目录：" + bundleJsDir)
        let fileManager = FileManager.default
        try! fileManager.createDirectory(atPath: bundleJsDir, withIntermediateDirectories: true, attributes: nil)
        localJs = bundleJsDir + localJs
//        print("localJs：" + localJs)
        if(fileManager.fileExists(atPath: localJs)) {
//            print("localJs存在：" + localJs)
            callback("file://" + localJs, false)
        } else {
//            print("localJs不存在，开始下载：" + remoteJs)
            //进行下载文件到本地
            let request = URLRequest(url: URL.init(string: remoteJs)!)
            let session = URLSession.shared
            let dataTask = session.dataTask(with: request,
                                            completionHandler: {(data, response, error) -> Void in
                                                if error != nil{
                                                    callback(false, false)
                                                }else{
//                                                    print("remoteJs下载成功：" + remoteJs)
                                                    fileManager.createFile(atPath: localJs, contents: data, attributes: nil)
                                                    callback("file://" + localJs, false)
                                                }
            }) as URLSessionTask
            //使用resume方法启动任务
            dataTask.resume()
        }
    }
}
